/***
 * Author: Andrey Zhmaylik
 * 		   Erez Bar
 * 
 * What:   Settings change screen.
 */

package house.keepper;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Settings extends Activity {
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        String email,phone;
        try {
			Connection.s_out.writeInt(ServerCommands.ChangeSettings.value());
			email = Connection.s_in.readUTF();
			phone = Connection.s_in.readUTF();
			((EditText)findViewById(R.id.email_edit_text)).setText(email);
			((EditText)findViewById(R.id.phone_edit_text)).setText(phone);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Button btn = (Button)findViewById(R.id.ok_settings);
        
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	SetSettings();
            }
                   
        });
        btn = (Button)findViewById(R.id.cancel_settings);
        
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	CloseSettings();
            }
                   
        });
	}

	protected void CloseSettings() {
		Intent i = new Intent(this, MainScreen.class);
        startActivity(i);	
	}

	protected void SetSettings() {
		try {
			String email = ((EditText)findViewById(R.id.email_edit_text)).getText().toString();
			String phone = ((EditText)findViewById(R.id.phone_edit_text)).getText().toString();
			if(CheckEmail(email))
			{
				Connection.s_out.writeInt(ServerCommands.SetSettings.value());
				Connection.s_out.writeUTF(email);
				Connection.s_out.writeUTF(phone);
				Connection.s_out.flush();
				this.CloseSettings();
			}
			else
			{
				((EditText)findViewById(R.id.email_edit_text)).setText("");
				TextView tv = new TextView(this);
	    		tv.setText("Please enter a valid email address");
	    		tv.setTextSize(20);
	    		android.app.Dialog alertWindow = new android.app.Dialog(this);
	    		alertWindow.setTitle("Error");
	    		alertWindow.setContentView(tv);
	    		alertWindow.show();
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private boolean CheckEmail(String email) {
		if(email != null)
        {
            //Set the email pattern string
            Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
            //Match the given string with the pattern
            Matcher m = p.matcher(email);
            
            return m.matches();
        }       
        return false;
	}
}
